package com.financing.platform.web.api;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;

import com.financing.platform.service.IFpFundCMService;
import com.financing.platform.service.IFpFundCompanyService;
import com.financing.platform.service.IFpFundInfoService;
import com.financing.platform.service.IFpFundMCService;
import com.financing.platform.service.IFpFundMFService;
import com.financing.platform.service.IFpFundManagerService;
import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.util.DTOdateSet;
import com.financing.platform.util.StringUtil;
import com.financing.platform.dto.FpFundCMDTO;
import com.financing.platform.dto.FpFundCompanyDTO;
import com.financing.platform.dto.FpFundInfoDTO;
import com.financing.platform.dto.FpFundMCDTO;
import com.financing.platform.dto.FpFundMFDTO;
import com.financing.platform.dto.FpFundManagerDTO;

/**
 * @Description: Controller
 * @author caojiayao
 * @version 1.0
 * @created 
 */
@Controller
@RequestMapping("/api/fund/")
@Scope("prototype")
public class FpFundManagerAction  extends BaseAction {
	@Autowired
	private IFpFundManagerService fpFundManagerService;
	
	@Autowired
	private IFpFundMFService fpFundMFService;
	
	@Autowired
	private IFpFundInfoService fpFundInfoService;
	
	@Autowired
	private IFpFundCompanyService fpFundCompanyService;
	
	@Autowired
	private IFpFundMCService fpFundMCService;
	
	@Autowired
	private IFpFundCMService fpFundCMService;
	
	@RequestMapping("/fdManagerQueryList.shtml")
	@ResponseBody
	public ResultMap queryList(HttpServletRequest request,
			HttpServletResponse response,FpFundManagerDTO dto,Pager pager) {
		try{
			log.info("Begin query fa user list!");
			
			dto.setBegin(pager.getStartIndex());
			dto.setPageSize(pager.getPageSize());
			
			int count = fpFundManagerService.countFpFundManager(dto);
			pager.setTotalCount(count);
			List<FpFundManagerDTO> list = fpFundManagerService.listFpFundManager(dto);
			
			resultMap.setAttribute("list",list);
			resultMap.setAttribute("pager", pager);
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			
			log.info("End querylist!");
		}catch(Exception e){
			log.error("queryList error !",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
			
			return resultMap;
	}
	
	@RequestMapping("/fdManagerAdd.shtml")
	@ResponseBody
	public ResultMap add(HttpServletRequest request,
			HttpServletResponse response,FpFundManagerDTO dto,String jjxxIds,String beforeJjgsIds,String nowJjgsId) {
		try{
			log.info("Begin add info !");
			
			//参数缺失
			if(StringUtil.isNullOrEmptyStr(dto.getJjjlbm())){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				return resultMap;
			}
			
			//重复添加
			if(isRepeat(dto.getJjjlbm())){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_REPEAT_LOSE);
				return resultMap;
			}
			
			DTOdateSet.setCreateInfo(dto, request.getSession());
			
			Integer id =fpFundManagerService.insertFpFundManager(dto);
			
			//add 基金经理关联基金信息
			if(jjxxIds!=null)
			this.addFpFundMF(jjxxIds, dto.getJjjlbm());
			
			// add 基金经理任职过的公司
			if(beforeJjgsIds != null)
			this.addFpFundMC(beforeJjgsIds,dto.getJjjlbm());
			
			// add 基金公司下属基金经理
			if(nowJjgsId != null)
			this.addFpFundCM(nowJjgsId,dto.getJjjlbm(),dto.getJjjlmc());
			
			resultMap.getAttribute().put("id",id);
			
			if(id !=null && id >0 ){
			    resultMap.setStatus(STATUS.SUCCESS);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			}else{
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
			}
			log.info("End add fa user info !");
		}catch(Exception e){
			log.error("update fa user info error!",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
			return resultMap;
	}
	
	@RequestMapping("/fdManagerUpdate.shtml")
	@ResponseBody
	public ResultMap update(HttpServletRequest request,
			HttpServletResponse response,FpFundManagerDTO dto,String jjxxIds,String beforeJjgsIds,String nowJjgsId) {
		try{
			log.info("Begin update fa user info !");
			
			//参数缺失
			if(StringUtil.isNullOrEmptyStr(dto.getJjjlbm())){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				return resultMap;
			}
			
			//重复添加
			if(isRepeatUpdate(dto.getJjjlbm(),dto.getId())){
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.STATUS_CODE_REPEAT_LOSE);
				return resultMap;
			}
			
			DTOdateSet.setUpdateInfo(dto, request.getSession());
			
			int count = fpFundManagerService.updateFpFundManager(dto);
			
			//update 基金经理关联基金信息
			if(jjxxIds != null)
			this.updateFpFundMF(jjxxIds, dto.getJjjlbm());
			
			// update 基金经理任职过的公司
			this.updateFpFundMC(beforeJjgsIds,dto.getJjjlbm());
			
			// update 基金公司下属基金经理
			this.updateFpFundCM(nowJjgsId,dto.getJjjlbm(),dto.getJjjlmc());
			
			resultMap.getAttribute().put("update count",count);
			if(count >0){
			    resultMap.setStatus(STATUS.SUCCESS);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			}else{
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
			}
			log.info("End update fa user info !");
		}catch(Exception e){
			log.error("update fa user info error!",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
			return resultMap;
				
	}
	
	/**
	 * 判断重复
	 * @Title: isRepeat
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param @param jjbm
	 * @return Boolean 返回类型
	 * @throws
	 */
	private Boolean isRepeat(String jjjlbm){
		FpFundManagerDTO dto = new FpFundManagerDTO();
		dto.setJjjlbm(jjjlbm);
		if(fpFundManagerService.getFpFundManager(dto) == null){
			return false;
		}
		return true;
	} 
	
	/**
	 * 判断重复
	 * @Title: isRepeat
	 * @Description: TODO(这里用一句话描述这个方法的作用)
	 * @param @param jjbm
	 * @return Boolean 返回类型
	 * @throws
	 */
	private Boolean isRepeatUpdate(String jjbm,Integer id){
		FpFundManagerDTO dto = new FpFundManagerDTO();
		dto.setJjjlbm(jjbm);
		dto = fpFundManagerService.getFpFundManager(dto);
		if(dto != null && dto.getId().intValue() != id.intValue()){
			return true;
		}
		return false;
	}
	
	@RequestMapping("/fdManagerDelete.shtml")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request,
			HttpServletResponse response,FpFundManagerDTO dto) {
		try{
			log.info("Begin delete fa user info !");
			int count = fpFundManagerService.delFpFundManager(dto.getId());
			
			resultMap.getAttribute().put("delete count",count);
			
			if(count > 0){
			    resultMap.setStatus(STATUS.SUCCESS);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			}else{
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
			}
			log.info("End delete fa user info !");
		}catch(Exception e){
			log.error("delete fa user info error!",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
			
			return resultMap;
				
	}
	
	//基金经理关联基金信息
	private void addFpFundMF(String jjxxIds,String jjjlbm){ 
		String[] ids = jjxxIds.split(",");
		for (String id : ids) {
			if (!StringUtil.isNullOrEmptyStr(id)) {
				FpFundInfoDTO dto = new FpFundInfoDTO();
				dto.setId(new Integer(id));
				// get 基金信息
				FpFundInfoDTO fundInfoDTO = fpFundInfoService.getFpFundInfo(dto);
				if (fundInfoDTO != null && fundInfoDTO.getJjbm() != null) {
					// add 基金经理关联基金信息
					FpFundMFDTO fundMFDTO = new FpFundMFDTO();
					fundMFDTO.setJjbm(fundInfoDTO.getJjbm());
					fundMFDTO.setJjjlbm(jjjlbm);
					fundMFDTO.setJjmc(fundInfoDTO.getJjmc());
					
					fpFundMFService.insertFpFundMF(fundMFDTO);
				}
			}
		}
	}
	
	//add 基金经理关联基金公司
	private void addFpFundMC(String jjgsIds,String jjjlbm){
		String[] ids = jjgsIds.split(",");
		for (String id : ids) {
			if (!StringUtil.isNullOrEmptyStr(id)) {
				FpFundCompanyDTO dto = new FpFundCompanyDTO();
				dto.setId(new Integer(id));
				//查询基金公司
				FpFundCompanyDTO fundCompanyDTO = fpFundCompanyService.getFpFundCompany(dto);
				
				if (fundCompanyDTO != null && fundCompanyDTO.getJjgsbm() != null) {
					// add 基金经理关联基金公司
					FpFundMCDTO fundMCDTO = new FpFundMCDTO();
					fundMCDTO.setJjjlbm(jjjlbm);
					fundMCDTO.setJjgsbm(fundCompanyDTO.getJjgsbm());
					fundMCDTO.setJjgsmc(fundCompanyDTO.getGsmc());
					fundMCDTO.setJjgslx(fundCompanyDTO.getGslx());

					fpFundMCService.insertFpFundMC(fundMCDTO);
				}
			}
		}
	}
	
	//add 基金公司下属基金经理
	private void addFpFundCM(String jjgsId,String jjjlbm,String jjjlmc){
		if (!StringUtil.isNullOrEmptyStr(jjgsId)) {
			FpFundCompanyDTO dto = new FpFundCompanyDTO();
			dto.setId(new Integer(jjgsId));
			//查询基金公司
			FpFundCompanyDTO fundCompanyDTO = fpFundCompanyService.getFpFundCompany(dto);
			
			if (fundCompanyDTO != null && fundCompanyDTO.getJjgsbm() != null) {
				// add 基金经理关联基金公司
				FpFundCMDTO fundCMDTO = new FpFundCMDTO();
				fundCMDTO.setJjjlbm(jjjlbm);
				fundCMDTO.setJjgsbm(fundCompanyDTO.getJjgsbm());
				fundCMDTO.setJlmc(jjjlmc);

				fpFundCMService.insertFpFundCM(fundCMDTO);
			}
		}
	}
	
	
	@RequestMapping("/fdManagerDetail.shtml")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request,
			HttpServletResponse response, FpFundManagerDTO requestDto) {
		try{
			FpFundManagerDTO dto = fpFundManagerService.getFpFundManager(requestDto);
			
			//曾经任职的公司
			List<FpFundCompanyDTO> beforeCompanyList = new ArrayList<FpFundCompanyDTO>();
			//现在任职的公司
			FpFundCompanyDTO nowCompany = new FpFundCompanyDTO();
			//管理的基金
			List<FpFundInfoDTO> fundList = new ArrayList<FpFundInfoDTO>();
			if(dto != null && !StringUtil.isNullOrEmptyStr(dto.getJjjlbm())){
				beforeCompanyList = fpFundCompanyService.listFpFundCompanyByManagerId(dto.getJjjlbm());
				nowCompany = fpFundCompanyService.getFpFundCompanyByManagerId(dto.getJjjlbm());
				fundList = fpFundInfoService.listFpFundInfoByManagerId(dto.getJjjlbm());
			}
			
			
			resultMap.setAttribute("detail", dto);
			resultMap.setAttribute("nowCompany", nowCompany);
			resultMap.setAttribute("beforeCompanyList", beforeCompanyList);
			resultMap.setAttribute("fundList", fundList);
			
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			log.error("query friendshipLink list error",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return resultMap;
	}
	
	//基金经理关联基金信息
	private void updateFpFundMF(String jjxxIds,String jjjlbm){ 
		//删除
		FpFundMFDTO paramDto = new FpFundMFDTO();
		paramDto.setJjjlbm(jjjlbm);
		List<FpFundMFDTO> cfdtos =  fpFundMFService.listFpFundMF(paramDto);
		for(FpFundMFDTO ddto : cfdtos){
			fpFundMFService.delFpFundMF(ddto.getId());
		}
		
		//添加
		this.addFpFundMF(jjxxIds, jjjlbm);
	}
		
	//update 基金经理关联基金公司
	private void updateFpFundMC(String jjgsIds,String jjjlbm){
		//删除
		FpFundMCDTO paramDto = new FpFundMCDTO();
		paramDto.setJjjlbm(jjjlbm);
		List<FpFundMCDTO> cfdtos =  fpFundMCService.listFpFundMC(paramDto);
		for(FpFundMCDTO ddto : cfdtos){
			fpFundMCService.delFpFundMC(ddto.getId());
		}
		
		if(jjgsIds != null){
		//添加
		this.addFpFundMC(jjgsIds, jjjlbm);
		}
	}
	
	//update 基金公司下属基金经理
	private void updateFpFundCM(String jjgsId,String jjjlbm,String jjjlmc){
		//删除
		FpFundCMDTO paramDto = new FpFundCMDTO();
		paramDto.setJjjlbm(jjjlbm);
		List<FpFundCMDTO> cfdtos =  fpFundCMService.listFpFundCM(paramDto);
		for(FpFundCMDTO ddto : cfdtos){
			fpFundCMService.delFpFundCM(ddto.getId());
		}
		
		if(jjgsId != null){
		//添加
		this.addFpFundCM(jjgsId, jjjlbm,jjjlmc);
		}
	}
}
